﻿#include "riggerscene.h"
#include <QMenu>
#include <QRectF>
#include <QGraphicsItem>
#include <QDebug>
#include <QGraphicsSceneContextMenuEvent>
RiggerScene::RiggerScene(const QSizeF& size, int& type, QObject* parent)
	: QGraphicsScene(QRectF(0, 0, size.width(), size.height()), parent),
	ItemType(type)
{
	setBackgroundBrush(QBrush(Qt::transparent));
	setForegroundBrush(QBrush(Qt::transparent));


}

RiggerScene::~RiggerScene()
{

}

void RiggerScene::resizeScene(QRectF rect)
{
	setSceneRect(rect);
}

void RiggerScene::judgmentAdsorption(QPointF childItemPos)
{
	QPointF itemParentPos = QPointF(0, 0);
	if (((childItemPos.x() - itemParentPos.x() >= -ADSORPTION_VALUE) && (ADSORPTION_VALUE >= (childItemPos.x() - itemParentPos.x())))
		&& ((childItemPos.y() - itemParentPos.y() >= -ADSORPTION_VALUE) && (ADSORPTION_VALUE >= (childItemPos.y() - itemParentPos.y())))) {
		AnchorItem* itemParent = new AnchorItem();
		childItem->setPos(QPointF(0, 0));
		qDebug() << childItem->mapFromScene(itemParentPos);
	}
	else if (((childItemPos.y() - itemParentPos.y() >= -ADSORPTION_VALUE) && (childItemPos.y() - itemParentPos.y() <= ADSORPTION_VALUE))) {
		AnchorItem* itemParent = new AnchorItem();
		childItem->setPos(QPointF(0, 0));
		qDebug() << childItem->mapFromScene(itemParentPos);
	}
	else if (((childItemPos.x() - itemParentPos.x() >= -ADSORPTION_VALUE) && (ADSORPTION_VALUE >= (childItemPos.x() - itemParentPos.x())))) {
		AnchorItem* itemParent = new AnchorItem();
		childItem->setPos(QPointF(0, 0));
		qDebug() << childItem->mapFromScene(itemParentPos);
	}
	update();
}

void RiggerScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
	QGraphicsScene::mousePressEvent(event);
}

void RiggerScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
	connect(childItem, &AnchorItem::itemPos, this, &RiggerScene::judgmentAdsorption);
	QGraphicsScene::mouseMoveEvent(event);
}

void RiggerScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
	QGraphicsScene::mouseReleaseEvent(event);
}

void RiggerScene::keyPressEvent(QKeyEvent* event)
{
	if (event->key() == Qt::Key_Delete)
	{
		QList<QGraphicsItem*> list = selectedItems();
		if (list.count())
		{
			foreach(QGraphicsItem * item, list)
			{
				removeItem(item);
			}
		}
	}

	QGraphicsScene::keyPressEvent(event);
}

void RiggerScene::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
	QMenu menu;
	QAction* action = menu.addAction(QIcon(":/icons/add_plus.png"), QString("添加图形"));
	if (menu.exec(QCursor::pos()) == action)
	{
		QPen pen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
		switch (ItemType = DEF2_ITEM) {
		case ELLIPSE_ITEM:
		{
			QGraphicsEllipseItem* ellipse = new QGraphicsEllipseItem(QRectF(event->scenePos(), QSizeF(200, 200)));
			ellipse->setAcceptHoverEvents(true);
			ellipse->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			pen.setWidth(1);
			ellipse->setPen(pen);
			ellipse->setBrush(QBrush(Qt::red));
			ellipse->setOpacity(0.8);
			addItem(ellipse);
		}
		break;
		case LINE_ITEM:
		{
			QGraphicsLineItem* line = new QGraphicsLineItem(QLineF(event->scenePos(), event->scenePos() + QPointF(100, 50)));
			line->setAcceptHoverEvents(true);
			line->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			pen.setWidth(3);
			line->setPen(pen);
			line->setOpacity(0.8);
			addItem(line);
		}
		break;
		case RECT_ITEM:
		{
			QGraphicsRectItem* rect = new QGraphicsRectItem(QRectF(event->scenePos(), QSizeF(200, 150)));
			rect->setAcceptHoverEvents(true);
			rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			pen.setWidth(1);
			rect->setPen(pen);
			rect->setBrush(QBrush(Qt::red));
			rect->setOpacity(0.8);
			addItem(rect);
		}
		break;
		case IMAGE_ITEM:
		{
			QGraphicsPixmapItem* pixmap = new QGraphicsPixmapItem(QPixmap(":/icons/circle_green.png"));
			pixmap->setAcceptHoverEvents(true);
			pixmap->setPos(event->scenePos());
			pixmap->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			addItem(pixmap);
		}
		break;
		case ANCHOR_ITEM:
		{
			AnchorItem* item = new AnchorItem(QRectF(event->scenePos(), QSizeF(150, 150)));
			addItem(item);
		}
		break;
		case DEF1_ITEM:
		{
			QPixmap map = QPixmap(":/icons/Circle1.png");
			AnchorItem* item = new AnchorItem(QRectF(event->scenePos(), map.size()), map);
			addItem(item);
		}
		break;
		case DEF2_ITEM:
		{
			childItem = new AnchorItem(QRectF(event->scenePos(), QSizeF(100, 150)), QPixmap(":/FGF"));
			addItem(childItem);
		}
		break;
		default:
			break;
		}
	}

	QGraphicsScene::contextMenuEvent(event);
}

void RiggerScene::wheelEvent(QGraphicsSceneWheelEvent* event)
{
	QGraphicsScene::wheelEvent(event);
}

void RiggerScene::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
{
	event->accept();
	QGraphicsScene::dragEnterEvent(event);
}

void RiggerScene::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
{
	event->accept();
}

void RiggerScene::dropEvent(QGraphicsSceneDragDropEvent* event)
{
	event->acceptProposedAction();
	QGraphicsScene::dropEvent(event);
}

